package com.vogue.supplymall.order.domain.provider;

import com.vogue.ssm.database.provider.BaseSqlProvider;
import com.vogue.ssm.database.util.DealStatus;
import com.vogue.supplymall.common.domain.dto.SearchRefundInfo;
import org.apache.commons.lang3.StringUtils;

/**
 * The type Refund order list sql proveder.
 */
public class RefundOrderListSqlProveder extends BaseSqlProvider {
    /**
     * 得到退单主列表的sql
     *
     * @param searchRefundInfo the search refund info
     * @return the string
     */
    public String findRefundOrder(SearchRefundInfo searchRefundInfo) {
        StringBuilder sql = new StringBuilder();

        sql.append("SELECT ")
                .append(" ro.id orderid,")
                .append("ro.refundnumber ordernumber,")
                .append("ro.createdate ordercreatedate,")
                .append("rs.id statusId,")
                .append("CONCAT(rs.refundstatus,'') billState,")
                .append("rs.sellerpullstate sellpullstate,")
                .append("rs.buyerpullstate buypullstate,")
                .append("ro.finalprice orderprice,")
                .append("1 as totalquantity,")
                .append("ro.freight,")
                .append("os.organid groupId,")
                .append("os.shopid shopId,")
                .append("os.entname groupName,")
                .append("os.shopname shopName,")
                .append("ob.btel buytel,")
                .append("ob.storename buyorganname,")
                .append("ob.buyertype,")
                .append("ifnull(rp.paymentmode,'0') paymentmode,")
                .append("oip.price itemprice,")
                .append("rd.refundquantity itemquantity,")
                .append("oip.imgurl,")
                .append("oip.pname,")
                .append("oip.punit unitname,")
                .append("oi.storageid,")
                .append("oi.storage")
                .append(" from refund_order ro")
                .append(" join refund_detail rd on rd.refundid=ro.id")
                .append(" join `order` o on o.id=ro.orderid")
                .append(" join order_item oi on oi.orderid=o.id")
                .append(" join refund_status rs on rs.refundid=ro.id")
                .append(" join order_seller os on o.id=os.orderid")
                .append(" join order_buyer ob on o.id=ob.orderid")
                .append(" left join refund_payment rp on rp.refundid=ro.id")
                .append(" JOIN order_item_product oip on o.id=oip.orderid and oi.id=oip.orderitemid and oip.id=rd.oipid")
                .append(getWhereOfRefundOrder(searchRefundInfo));

        return DealStatus.formatShadeSql(sql.toString());
    }

    /**
     * Gets where of refund order.
     *
     * @param searchRefundInfo the search refund info
     * @return the where of refund order
     */
    public String getWhereOfRefundOrder(SearchRefundInfo searchRefundInfo) {
        StringBuilder whereSql = new StringBuilder();

        whereSql.append(" where")
                .append(" ro.disabledflag=0");

        if (searchRefundInfo.getOrderStatus() != null) {
            whereSql.append(" AND rs.refundstatus = #{orderStatus} ");
        }

        if (searchRefundInfo.isIfBuyer()) {
            whereSql.append(" AND ob.syscodeid = #{syscodeid} ")
                    .append(" AND ob.organid = #{organid} ")
                    .append(" AND ob.userid = #{loginId} ");
        } else {
            whereSql.append(" AND os.syscodeid = #{syscodeid} ")
                    .append(" AND os.organid = #{organid} ");
        }

        // 匹配搜索内容
        if (StringUtils.isNotEmpty(searchRefundInfo.getKeyword())) {
            whereSql.append(" AND ( ro.refundnumber LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" OR os.shopname LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" OR oip.pname LIKE CONCAT('%',#{keyword},'%') ")
                    .append(" ) ");
        }
        /*if(searchRefundInfo.isSellerpull()) {
        	whereSql.append(" AND rs.sellerpullstate = 1 ");
        }else {
        	whereSql.append(" AND rs.sellerpullstate <> 1 ");
        }*/

        //下单开始时间
        if (StringUtils.isNotEmpty(searchRefundInfo.getBeginDate())) {
            whereSql.append(" AND ro.createdate >= #{beginDate} ");
        }
        //下单结束时间
        if (StringUtils.isNotEmpty(searchRefundInfo.getEndDate())) {
            whereSql.append(" AND ro.createdate <= #{endDate} ");
        }

        whereSql.append(" ORDER BY rs.modifydate DESC ")
                .append(" LIMIT #{start},#{length} ");


        return whereSql.toString();
    }

}
